package com.vive.tree;

import com.vive.book.Book;
import org.apache.wicket.extensions.markup.html.tree.table.*;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.link.Link;

import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeModel;

public class TreeTablePage extends WebPage {

    public TreeTablePage() {
    	super();

        IColumn columns[] = new IColumn[]{
                new PropertyTreeColumn(new ColumnLocation(ColumnLocation.Alignment.LEFT,18, ColumnLocation.Unit.EM),"作者","userObject.author"),
                new PropertyRenderableColumn(new ColumnLocation(ColumnLocation.Alignment.LEFT,12, ColumnLocation.Unit.EM),"标题","userObject.title"),
        };

        final TreeTable tree = new TreeTable("treeTable", buildTreeModel(),columns);
        tree.getTreeState().setAllowSelectMultiple(true);
        add(tree);

        add(new Link("expandAll") {
            @Override
            public void onClick() {
                tree.getTreeState().expandAll();
            }
        });

        add(new Link("collapseAll") {
            @Override
            public void onClick() {
                tree.getTreeState().collapseAll();
            }
        });

        }

    private TreeModel buildTreeModel() {
        Book rootBook = new Book();
        rootBook.setAuthor("root作者");
        rootBook.setTitle("root标题");

        DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode(rootBook);

        for (int i = 0; i < 10; i++) {
            Book parentBook = new Book();
            DefaultMutableTreeNode parentNode = new DefaultMutableTreeNode(parentBook);

            parentBook.setAuthor("父作者-" + i);
            parentBook.setTitle("父标题-" + i);

            for (int j = 0; j < 5; j++) {
                Book childBook = new Book();
                childBook.setAuthor("子作者-" + i + "-" + j);
                childBook.setTitle("子标题-" + i + "-" + j);

                parentNode.add(new DefaultMutableTreeNode(childBook));
            }
            rootNode.add(parentNode);
        }
        return new DefaultTreeModel(rootNode);
    }
}

